use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(UserType::Table)
                    .if_not_exists()
                    .col( ColumnDef::new(UserType::Id).integer().not_null().auto_increment().primary_key().comment("id") )
                    .col( ColumnDef::new(UserType::Label).string_len(10).not_null().unique_key().comment("名称") )
                    // 固定字段
                    .col( ColumnDef::new(UserType::CreateUser).integer().not_null().comment("创建人") )
                    .col( ColumnDef::new(UserType::UpdateUser).integer().comment("修改人") )
                    .col( ColumnDef::new(UserType::CreateTime).timestamp().default(Expr::current_timestamp()).comment("创建时间") )
                    .col( ColumnDef::new(UserType::UpdateTime).timestamp().default(Expr::current_timestamp()).extra("on update current_timestamp").comment("更新时间") )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.drop_table(Table::drop().table(UserType::Table).to_owned()).await
    }
}

#[derive(DeriveIden)]
enum UserType {
    Table,
    Id,
    Label,
    CreateUser,
    UpdateUser,
    CreateTime,
    UpdateTime,
}